package com.aiapphub.BaseCode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aiapphub.BaseCode.common.ResponseResult;
import com.aiapphub.BaseCode.entity.SsoUser;
import com.aiapphub.BaseCode.mapper.SsoUserMapper;
import com.aiapphub.BaseCode.service.SsoUserService;
import com.aiapphub.BaseCode.utils.BeanCopyUtils;
import com.aiapphub.BaseCode.utils.JwtUtil;
import com.aiapphub.BaseCode.vo.PageVo;
import com.aiapphub.BaseCode.vo.SsoUserInfoVo;
import com.aiapphub.BaseCode.vo.UserPageInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户信息表(SsoUser)表服务实现类
 *
 * @author makejava
 * @since 2024-10-09 10:05:50
 */
@Service("ssoUserService")
public class SsoUserServiceImpl extends ServiceImpl<SsoUserMapper, SsoUser> implements SsoUserService {


    @Autowired
    private SsoUserMapper ssoUserMapper;

    @Override
    public ResponseResult getUserInfo(String token) {
        System.out.println("token:"+token);
        try {
            // 解析token获取用户id
            Claims claims = JwtUtil.parseJWT(token);
            if (claims == null || !claims.getSubject().matches("\\d+")) {
                return ResponseResult.errorResult(500, "Token无效");
            }
            Long userId = Long.parseLong(claims.getSubject());
            System.out.println("userId:"+userId);
            // 根据用户id查询用户信息
            LambdaQueryWrapper<SsoUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SsoUser::getLoginName, userId);
            SsoUser ssoUser = ssoUserMapper.selectOne(queryWrapper);
            if (ssoUser == null) {
                return ResponseResult.errorResult(500, "用户不存在");
            }
            // 封装成UserInfoVo
            SsoUserInfoVo userInfoVo = BeanCopyUtils.copyBean(ssoUser, SsoUserInfoVo.class);
            return ResponseResult.okResult(userInfoVo);
        } catch (JwtException e) {
            // 特定的JWT解析异常
            return ResponseResult.errorResult(500, "Token解析失败");
        } catch (NumberFormatException e) {
            // 用户ID解析异常
            return ResponseResult.errorResult(500, "Token中用户ID格式错误");
        } catch (Exception e) {
            // 其他异常
            return ResponseResult.errorResult(500, "未知错误");
        }
    }

    @Override
    public List<UserPageInfoVo> getAllUsers() {
        return null;
    }



    @Override
    public PageVo selectUserPage(Integer pageNum, Integer pageSize) {
        // 创建查询条件
        LambdaQueryWrapper<SsoUser> queryWrapper = new LambdaQueryWrapper<>();

        // 构建分页对象，指定页码和页面大小
        Page<SsoUser> page = new Page<>(pageNum, pageSize);

        // 执行分页查询，传入查询条件
        page(page, queryWrapper);

        // 将查询结果的 User 实体对象转换为 UserVo（视图对象）
        List<UserPageInfoVo> userPageVos = BeanCopyUtils.copyBeanList(page.getRecords(), UserPageInfoVo.class);

        // 构建并返回分页结果，包含数据列表和总记录数
        PageVo pageVo = new PageVo(userPageVos, page.getTotal());

        return pageVo;
    }



}

